package com.ubxtech.db.util;

import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.ubxtech.core.context.ContextUtil;
import com.ubxtech.core.utils.StrPool;

import java.lang.reflect.Field;

/**
 * @author Victor.Xiao
 * @since 2023-11-09 20:44
 **/
public class CustomReflectUtil {

    /**
     * 设置字段值,如果有值则不设置,没有值才覆盖
     * @author Victor.Xiao
     * @since 2023-11-09 20:52
     * @param metaObject metaObject
     * @param fieldName fieldName
     * @param value value
    */
    public static void setFieldValue(Object metaObject, String fieldName, String value) {
        Field field = ReflectUtil.getField(metaObject instanceof Class ? (Class<?>) metaObject : metaObject.getClass(), fieldName);
        if (field == null) {
            return;
        }
        Object oldVal = ReflectUtil.getFieldValue(metaObject,field);
        if (oldVal == null || StrUtil.isBlank(oldVal.toString()) || StrPool.ZERO.equalsIgnoreCase(oldVal.toString())) {
            if (StrUtil.isNotBlank(value)) {
                ReflectUtil.setFieldValue(metaObject, field, value);
            }
        }
    }
}
